package com.pokermind.operator.config;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * AI服务配置类 - 基于Spring AI框架
 * 支持阿里云Qwen (通义千问)
 * AI失败时自动使用Math Fallback（本地决策）
 * 
 * @author PokerMind Team
 */
@Configuration
@ConditionalOnClass(ChatModel.class)
public class AIConfig {
    
    private static final Logger logger = LoggerFactory.getLogger(AIConfig.class);
    
    @Value("${spring.ai.dashscope.chat.options.model:qwen-turbo}")
    private String qwenModel;

    /**
     * Qwen ChatClient
     */
    @Bean("qwenChatClient")
    @Primary
    public ChatClient qwenChatClient(@Autowired(required = false) DashScopeChatModel dashScopeModel) {
        if (dashScopeModel == null) {
            logger.warn("DashScopeChatModel not available");
            return null;
        }
        logger.info("========================================");
        logger.info("AI Configuration:");
        logger.info("  Provider: Qwen (Alibaba DashScope)");
        logger.info("  Model: {}", qwenModel);
        logger.info("  Fallback: Math-based local decision");
        logger.info("========================================");
        
        return ChatClient.builder(dashScopeModel)
            .defaultSystem("你是一个专业的德州扑克AI助手。请根据提供的牌面信息、位置、筹码情况等数据，给出最优的扑克决策。")
            .build();
    }
}
